APP = reader
NAME = Reader
VERSION = $(shell git describe --tags)
BUILD_TIME = $(shell date +%FT%T)
GIT_COMMIT = $(shell expr substr `git log --pretty=format:'%H' -n 1` 1 7)
PACKAGE = $(shell go list .)
COMMON_PACKAGE = ${PACKAGE}/cmd
BUILDFLAGS = -ldflags "-X '${COMMON_PACKAGE}.GitCommit=${GIT_COMMIT}' \
  -X '${COMMON_PACKAGE}.Version=${VERSION}' \
  -X '${COMMON_PACKAGE}.BuildTime=${BUILD_TIME}'"

VER = $(shell echo ${VERSION} | grep -Eo "[0-9]+\.[0-9]+\.[0-9]+")
DEB = dist/${APP}-${VER}

lint:
	golangci-lint run
	node_modules/.bin/ng lint

www:
	node_modules/.bin/ionic build --prod --engine=browser --platform=android

debug:
	node_modules/.bin/ionic build --engine=browser --platform=android
	go run main.go

serve:
	ionic serve -- --proxy-config proxy.conf.json

wire:
	wire gen gitee.com/xuender/reader/app

proto: protojs
	protoc --go_out=plugins=grpc:. pb/*.proto

protojs:
	./node_modules/.bin/pbjs -t static-module -w es6 --es6 -o src/pb.js pb/*.proto
	./node_modules/.bin/pbts -o src/pb.d.ts src/pb.js

clean:
	rm -rf www
	rm -rf dist

test:
	go test ./...
	node_modules/.bin/ng test --watch=false

windows:
	CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build $(BUILDFLAGS) -o dist/$(APP).exe main.go
	wixl -a x64 doc/windows.wxs -o dist/${APP}-${VER}.msi

darwin: www
	CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build $(BUILDFLAGS) -o dist/$(APP) main.go
	zip -j dist/${APP}-${VER}-darwin.zip dist/${APP}

linux: www
	CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build $(BUILDFLAGS) -o dist/$(APP) main.go
	zip -j dist/${APP}-${VER}-linux.zip dist/${APP}

	mkdir -p ${DEB}/usr/bin
	mkdir -p ${DEB}/DEBIAN

	cp dist/${APP} ${DEB}/usr/bin/${APP}

	echo "Package: ${APP}\n\
Version: ${VER}\n\
Section: base\n\
Priority: optional\n\
Architecture: amd64\n\
Maintainer: ender <xuender@139.com>\n\
Description: Directory remote read only." > ${DEB}/DEBIAN/control

	dpkg-deb --build ${DEB}

build: proto www wire windows darwin linux
